<?php

namespace App\Http\Controllers\Admin;

use App\Models\Staff;
use App\Models\StaffBonusPoolIncomeLog;
use App\Models\StaffIncomeLog;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class StaffController extends Controller
{

    public function index()
    {
       return view('admin.staff.index');
    }

    public function data(Request $request)
    {
        $model = Staff::query();
        if ($request->get('name')){
            $model = $model->where('name','like','%'.$request->get('name').'%');
        }
        if ($request->get('phone')){
            $model = $model->where('phone','like','%'.$request->get('phone').'%');
        }
        $res = $model->orderBy('created_at','desc')->paginate($request->get('limit',30))->toArray();
        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' => $res['total'],
            'data'  => $res['data']
        ];
        return response()->json($data);
    }

    public function create()
    {

        return view('admin.staff.create');
    }

    public function store(Request $request)
    {
        $data = $request->all();
        $data['uuid'] = \Faker\Provider\Uuid::uuid();

        if (Staff::create($data)){
            return redirect()->to(route('admin.staff'))->with(['status'=>'添加账号成功']);
        }
        return redirect()->to(route('admin.staff'))->withErrors('系统错误');
    }

    public function edit($id)
    {
        $staff = Staff::findOrFail($id);
        return view('admin.staff.edit',[
            'staff' => $staff
        ]);
    }

    public function update(Request $request, $id)
    {
        $staff = Staff::findOrFail($id);
        $data = $request->all();

        if ($staff->update($data)){
            return redirect()->to(route('admin.staff'))->with(['status'=>'更新用户成功']);
        }
        return redirect()->to(route('admin.staff'))->withErrors('系统错误');
    }

    public function destroy(Request $request)
    {
        $ids = $request->get('ids');
        if (empty($ids)){
            return response()->json(['code'=>1,'msg'=>'请选择删除项']);
        }
        if (Staff::destroy($ids)){
            return response()->json(['code'=>0,'msg'=>'删除成功']);
        }
        return response()->json(['code'=>1,'msg'=>'删除失败']);
    }

    public function getStaffIncomeLog(Request $request)
    {

      return view('admin.staffIncomeLog.index');
    }

    public function getStaffIncomeLogDate(Request $request)
    {

//        $query  = StaffBonusPoolIncomeLog::leftJoin("staff","staff_bonus_pool_income_log.staff_id","=","staff.id")
//            ->select("staff_bonus_pool_income_log.id","staff.name");
        $query = \DB::table("staff_bonus_pool_income_log as t1")
            ->leftJoin("staff as t2", "t1.staff_id","=","t2.id")
            ->select("t1.id", "t2.name", "t1.staff_id", "t1.money", "t1.proportion", "t1.created_at");

        if ($name = $request->input("name")){
            $query = $query->where("t2.name","like","%".$name."%");
        }
        if ($begin_time = $request->input('begin_time')){
            $query = $query->where("t1.created_at",">=", $begin_time);
        }
        if($end_time = $request->input("end_time")){
            $query = $query->where("t1.created_at", "<", $end_time);
        }

        $per_page = $request->query('per_page', 10);

        $res = $query->paginate($per_page)->toArray();

        $data = [
            'code' => 0,
            'msg'  => '正在请求中...',
            'count' => $res['total'],
            'data'  => $res['data']
        ];

        return response()->json($data);
    }
}
